package net.starwifi.duobao.utils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Created by ruo on 2015/3/27.
 */
public class MessageUtil {

    public MessageUtil() {
    }

    public static boolean isNullOrEmpty(String str) {
        return str == null || str.length() == 0;
    }

    public static String getJSONString(JSONObject object, String key) {
        String result = "";
        if(object.containsKey(key)) {
            result = object.getString(key);
        }

        return result;
    }

    public static int getJSONInteger(JSONObject object, String key) {
        int result = 1;
        if(object.containsKey(key)) {
            result = object.getInt(key);
        }

        return result;
    }

    public static JSONArray getJSONArray(JSONObject object, String key) {
        return object.containsKey(key)?object.getJSONArray(key):null;
    }

    public static boolean getJSONBoolean(JSONObject object, String key) {
        return object.containsKey(key)?object.getBoolean(key):true;
    }

    public static String convertXmlString(String content) {
        String result = content.replace("&", "&amp;");
        result = result.replace("\'", "&apos;");
        result = result.replace("\"", "&quot;");
        result = result.replace("<", "&lt;");
        result = result.replace(">", "&gt;");
        return result;
    }
}
